﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Linq;
using System.Text;
using SD.LLBLGen.Pro.ORMSupportClasses;
using CARMSData.CollectionClasses;
using CARMSData.EntityClasses;
using CARMSData.HelperClasses;


namespace CARMSBusiness
{
    /// <summary>
    /// Manage the dynamic system configurations.
    /// </summary>
    public class ConfigBusiness  
    {
    
        public static string GetConfiguration(string name)
        {
            var collection = new ConfigurationCollection();
            collection.GetMulti(ConfigurationFields.Name == name);
            if(collection.Count==1)
            {
                return collection[0].Value;
            }
            return string.Empty;
        }


        public static string GetConfiguration(int keyID)
        {
            var entity = new ConfigurationEntity(keyID);
            if (!entity.IsNew)
            {
                return entity.Value;
            }
            return string.Empty;
        }


   

        public static bool SaveConfig(ConfigurationEntity entity)
        {
            return entity.Save();
        }


        #region number index
        /// <summary>
        ///  默认返回 1000001
        /// </summary>
        /// <param name="strKeyID"></param>
        /// <returns></returns>
        public static int GetNumberIndexByKeyID(int strKeyID)
        {
            var entity = new ConfigurationEntity(strKeyID);
            if (!entity.IsNew)
            {
                return Convert.ToInt32(entity.Value);
            }
            else
            {
                entity.Name = "";
                entity.Oid = strKeyID;
                entity.Value = "1000001";
                entity.Save();
                return 1000001;
            }

        }

        public static bool UpdateNumberIndexByKeyID(int strKeyID)
        {
            var entity = new ConfigurationEntity(strKeyID);
            if (!entity.IsNew)
            {
                entity.Value = (Convert.ToInt32(entity.Value) + 1).ToString();
                return entity.Save();
            }
            return false;
        }

        ///// <summary>
        ///// Save all the configuration values.
        ///// </summary>
        //public virtual void SaveConfigurations(NameValueCollection configurations, ITransaction transaction)
        //{
        //    foreach (string name in configurations)
        //    {
        //        ModifyData(name, configurations[name], transaction);
        //    }
        //}

        ///// <summary>
        ///// Saves the configurations.
        ///// </summary>
        ///// <param name="configurations">The configurations.</param>
        //public virtual void SaveConfigurations(NameValueCollection configurations)
        //{
        //    ITransaction transaction = new Transaction(IsolationLevel.ReadCommitted, "Save Configuration");
        //    try
        //    {
        //        SaveConfigurations(configurations, transaction);
        //        transaction.Commit();
        //    }
        //    catch (Exception)
        //    {
        //        transaction.Rollback();
        //        throw;
        //    }
        //}

        //#region Overrides of CachableServiceBase<string>

        //protected override string OnLoadData(object key, ITransaction transaction)
        //{
        //    var metaData = new LinqMetaData(transaction);
        //    var query = from config in metaData.Configuration
        //                where
        //                    config.Name == (string)key
        //                select config.Value;
        //    var list = query.ToList();
        //    return list.Count > 0 ? list[0] : null;
        //}

        //protected override object GetDataKey(string data)
        //{
        //    return null;
        //}

        //protected override bool OnModifyData(object key, string data, ITransaction transaction)
        //{
        //    var collection = new ConfigurationCollection();
        //    transaction.Add(collection);
        //    collection.DeleteMulti(ConfigurationFields.Name == (string)key);
        //    if (data != null)
        //    {
        //        var config = new ConfigurationEntity { Value = data, Name = (string)key };
        //        transaction.Add(config);
        //        config.Save();
        //    }
        //    return true;
        //}
        #endregion
    }
}
